window.addEventListener("load", function() {
    //1:获取元素
    var focus = document.querySelector(".focus");
    var arrow_l = document.querySelector(".arrow-l");
    var arrow_r = document.querySelector(".arrow-r");
    //求focus的宽度
    var focusW = focus.offsetWidth;
    //定义num变量为当前为图片的索引
    var num = 0;
    //定义circle变量为当前小圆点的索引
    var circle = 0;
    //定义一个节流阀
    var flag = true;
    //2:绑定事件
    //2.1鼠标经过和离开左右切换显示和隐藏
    focus.addEventListener("mouseenter", function() {
        arrow_l.style.display = "block";
        arrow_r.style.display = "block";
        //鼠标经过时清除定时器
        clearInterval(timer);
        //清除定时器变量
        timer = null;
    })
    focus.addEventListener("mouseleave", function() {
            arrow_l.style.display = "none";
            arrow_r.style.display = "none";
            //鼠标离开时开启定时器
            timer = setInterval(function() {
                //手动调用右边切换按钮的点击事件
                arrow_r.click();
            }, 2000)
        })
        //2.2动态生成小圆圈，有几张图片就显示几个小圆圈
        //获取元素
    var ul = focus.querySelector("ul");
    var ol = focus.querySelector(".circle");
    //利用for循环遍历
    for (var i = 0; i < ul.children.length; i++) {
        //创建li
        var li = document.createElement("li");
        //设置索引
        li.setAttribute("data-index", i);
        //将创建好的li给ol
        ol.appendChild(li);
        //2.3排他思想，生成小圆圈的同时，添加绑定事件，点击小圆圈跳转到对应的图片
        li.addEventListener("click", function() {
            for (var i = 0; i < ol.children.length; i++) {
                ol.children[i].className = "";
            }
            this.className = "current";
            //取到当前索引
            var index = this.getAttribute("data-index");
            //把index给num
            num = index;
            //把index给circle
            circle = index;
            //设置动画
            animate(ul, -index * focusW);
        })
    }
    //克隆第一张图片
    var first = ul.children[0].cloneNode(true);
    //将第一张图片添加到ul中
    ul.appendChild(first);
    //var last=ul.children[ul.children.length-1];
    //将最后一张图片添加在前面
    //ul.insertBefore(last,ul.children[0]);
    //ul.appendChild(last);
    //把ol的第一个小圆点设置为初始值，初始样式
    ol.children[0].className = "current";
    //2.4点击右侧切换按钮，实现图片的滑动
    arrow_r.addEventListener("click", function() {
        if (flag) {
            flag = false;
            //判断num的值，防止超过了图片的长度
            if (num == ul.children.length - 1) {
                ul.style.left = 0;
                num = 0;
            }
            num++;
            animate(ul, -num * focusW, function() {
                //打开节流阀
                flag = true;
            });
            circle++;
            if (circle == ol.children.length) {
                circle = 0;
            }
            circleChange();
        }
    })
    arrow_l.addEventListener("click", function() {
        if (flag) {
            flag = false;
            //判断num的值，防止超过了图片的长度
            if (num == 0) {
                num = ul.children.length - 1;
                ul.style.left = -num * focusW + "px";
            }
            num--;
            animate(ul, -num * focusW, function() {
                //打开节流阀
                flag = true;
            });
            circle--;
            if (circle < 0) {
                circle = ol.children.length - 1;
            }
            //circle = circle < 0 ? ol.children.length - 1 : circle;
            circleChange();
        }
    })

    function circleChange() {
        //清除其他小圆点的样式
        for (var i = 0; i < ol.children.length; i++) {
            ol.children[i].className = "";
        }
        //给当前的小圆点添加样式
        ol.children[circle].className = "current";
    }

    //2.5自动播放轮播图
    var timer = setInterval(function() {
        //手动调用右边切换按钮的点击事件
        arrow_r.click();
    }, 2000)
})